﻿@model MVCCornucopia.Data.Enums.Gender?
@{    
               
    // [Description(Name="Some Name")] Attribute on your enum    
    Func<MVCCornucopia.Data.Enums.Gender, string> getDescription = en =>
    {
        Type type = en.GetType();
        System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
            if (attrs != null && attrs.Length > 0)
                return ((System.ComponentModel.DescriptionAttribute)attrs[0]).Description;
        }
        return en.ToString();
    };
    
  
    
    var listItems = MVCCornucopia.Data.Enums.Gender.GetValues(typeof(MVCCornucopia.Data.Enums.Gender)).OfType<MVCCornucopia.Data.Enums.Gender>().Select(e => new SelectListItem()
    {
        Text = getDescription(e),
        Value = e.ToString(),
        Selected = (Model.HasValue) ? e.Equals(Model.Value) : false
    });
  
    string prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    int index = 0;
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

<div class="editor-radio">

    @foreach (var li in listItems)
    {
       string fieldName = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);        
        @Html.RadioButton(prefix, li.Value, li.Selected, new { @id = fieldName })
        @Html.Label(fieldName, li.Text) 
        @Html.Raw("&nbsp;&nbsp;")
    }

</div>    

    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
                     
}